<template>
  <el-dialog class="loginorregister" width="500px" :visible.sync="visible" title="登陆/注册" append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
    <el-tabs type="border-card">
      <el-tab-pane label="登陆">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="login()"
          status-icon>
          <el-form-item prop="username">
            <el-input v-model="dataForm.username" :placeholder="$t('login.username')">
              <span slot="prefix" class="el-input__icon">
                <svg class="icon-svg" aria-hidden="true">
                  <use xlink:href="#icon-user"></use>
                </svg>
              </span>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="dataForm.password" type="password" :placeholder="$t('login.password')">
              <span slot="prefix" class="el-input__icon">
                <svg class="icon-svg" aria-hidden="true">
                  <use xlink:href="#icon-lock"></use>
                </svg>
              </span>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login()" class="w-percent-100">{{ $t('login.title') }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="注册">
        <el-form :model="dataForm2" :rules="dataRule2" ref="dataForm2" @keyup.enter.native="register()"
          status-icon>
          <el-form-item prop="username">
            <el-input v-model="dataForm2.username" :placeholder="$t('login.username')">
              <span slot="prefix" class="el-input__icon">
                <svg class="icon-svg" aria-hidden="true">
                  <use xlink:href="#icon-user"></use>
                </svg>
              </span>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="dataForm2.password" type="password" :placeholder="$t('login.password')">
              <span slot="prefix" class="el-input__icon">
                <svg class="icon-svg" aria-hidden="true">
                  <use xlink:href="#icon-lock"></use>
                </svg>
              </span>
            </el-input>
          </el-form-item>
          <el-form-item prop="customerName">
            <el-input v-model="dataForm2.customerName" placeholder="客户姓名"></el-input>
          </el-form-item>
          <el-form-item prop="mobile">
            <el-input v-model="dataForm2.mobile" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register()" class="w-percent-100">{{ $t('login.title') }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        username: '',
        password: ''
      },
      dataForm2: {
        username: '',
        password: '',
        customerName: '',
        mobile: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    },
    dataRule2 () {
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 显示弹窗
    init () {
      this.visible = true
    },
    // 登陆
    login: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.post('/app/login', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          Cookies.set('token', res.token)
          this.$emit('refresh')
          this.visible = false
        }).catch((error) => {
          console.log(error)
        })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 表单提交
    register: debounce(function () {
      this.$refs['dataForm2'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.post('/app/register', this.dataForm2).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$refs['dataForm2'].resetFields()
          this.$message.success(res.msg)
        }).catch((error) => {
          console.log(error)
        })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss">
.loginOrRegister{
  box-shadow: 0px 0px 7px rgba(0, 0, 0, 0.12);
  border-radius: 8px;
  .el-dialog__header{
    .el-dialog__title{
      font-size: 14px;
      font-weight: bold;
    }
    border-bottom: 1px solid #f2f2f2;
  }
  .el-dialog__body{
    padding: 15px 20px;
  }
}
</style>
